/**
 * 
 */
package org.wejde.muel;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * This class contains the JComponents and functions
 * related to the main (left-hand) portion of the GUI.
 * 
 * The JComponents in this class are the tabPane, the
 * additionalTextPane and chatroomPane.
 * 
 * The methods include retrieving text from the fieldPanes,
 * clear/resetting the fieldPanes, and get methods for the
 * chatroom textArea, submit button and clear button.
 * 
 * @author Eric Martin
 * @author Webb Pinner
 *
 */
public class mainPane extends JPanel {

	// The JPanels in the mainPane
	/**
	 * Contains the formfields.
	 */
	private tabPane tabs;
	
	/**
	 * Contains the addition textarea, submit button and clear button.
	 */
	private additionalTextPane additionalText;
	
	/**
	 * Contains the chatroom window.
	 */
	private chatroomPane chatroom;
		
	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = 2556160463659775644L;

	/**
	 * Used to build the gridbag layout constraints object.
	 * 
	 * @param gbc the gridbag constraint object
	 * @param gx the x-value in the grid
	 * @param gy the y-value in the grid
	 * @param gw the width of the cell
	 * @param gh the height of the cell
	 * @param wx the x-weight of the cell
	 * @param wy the y-weight of the cell
	 */
	private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
		
		gbc.gridx = gx;
		gbc.gridy = gy;
		gbc.gridwidth = gw;
		gbc.gridheight = gh;
		gbc.weightx = wx;
		gbc.weighty = wy;
	}
	
	/**
	 * Constructor Method
	 * 
	 * @param fa 2-Dimensional array of field objects
	 * @param faNames array of names to use for the tab
	 * names.
	 */
	public mainPane(fields[][] fa, String[] faNames) {
		super();
		this.setMinimumSize(new Dimension(600,375));
		this.setSize(new Dimension(this.getMinimumSize()));
		
		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		
		this.setLayout(gridBag);
				
		this.tabs = new tabPane(fa, faNames);
		this.additionalText = new additionalTextPane();
		this.chatroom = new chatroomPane();
	
		this.buildConstraints(constraints, 0, 0, 1, 1, 100, 90);
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		gridBag.setConstraints(tabs, constraints);
		this.add(this.tabs);

		this.buildConstraints(constraints, 0, 1, 1, 1, 100, 5);
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.anchor = GridBagConstraints.SOUTHWEST;
		gridBag.setConstraints(additionalText, constraints);
		this.add(this.additionalText);

		this.buildConstraints(constraints, 0, 2, 1, 1, 100, 5);
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.anchor = GridBagConstraints.SOUTHWEST;
		gridBag.setConstraints(chatroom, constraints);
		this.add(this.chatroom);
		
	}

	/**
	 * Get the JButton associated with the Submit Button.
	 *
	 * @return the Submit JButton object.
	 */
	public JButton getSubmit() {
		return this.additionalText.getSubmit();
	}

	/**
	 * Get the JButton associated with the Clear Button.
	 *
	 * @return the Clear JButton object.
	 */
	public JButton getClear() {
		return this.additionalText.getClear();
	}
	
	/**
	 * Issue the command to the additionalTextPane and 
	 * tabsPane to clear/reset the values from the input
	 * fields in the tabs and additional text window.
	 */
	public void clearFields() {
		this.tabs.clearFields();
		this.additionalText.clearFields();
	}
	
	/**
	 * Issue the command to the additionalTextPane and 
	 * tabsPane to get the values from the input fields in
	 * the tabs and additional text window.
	 */
	public String getFieldValues() {
		String msg ="";
		msg += this.tabs.getFieldValues();
		msg += this.additionalText.getAdditionalText();
		return msg;
	}
	
	/**
	 * Return the chatroom object.  This is needed to link
	 * the chatroom JTextArea to the XMPP client.
	 *  
	 * @return reference to the chatroomPane object.
	 */
	public chatroomPane getChatroomPane() {
		return this.chatroom;
	}
	
	/**
	 * Set the delimiter to use when collecting the values
	 * from the fields
	 * 
	 * @param delim String contain the delimiter.
	 */
	public void setDelimiter(String delim) {
		this.tabs.setDelimiter(delim);
	}
	
	/**
	 * Get the delimiter used when collecting the values
	 * from the field objects.
	 * 
	 * @return String containing the delimiter.
	 */
	public String getDelimiter() {
		return this.tabs.getDelimiter();
	}
}
